package dk.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ReturnData.ResponseData;

import dk.RedisUtils.RedisUtils;

@RestController
public class TokenController {
	@Autowired
	RedisUtils redisutils;
	//注册成功后传入userid后台颁发一个token给前台 这个方法不应该暴露给用户
	@RequestMapping("GetAdminTokenByid")
	public ResponseData GetAdminTokenByid(Integer id,Integer days) {
		
		ResponseData data= new ResponseData();
		//获得token
		String token = String.valueOf(UUID.randomUUID())+ System.currentTimeMillis();
		//如果数据库有"AdminToken"+id作为key的话 不做处理 如果没有继续下一步
		if(redisutils.IsExist("AdminToken"+id)) {
			data.setCode(201);
			data.setMessage("请不要在过期之前重复申请");
		}
		else {
			//存储进redis 双向存储并设置过期时间
			
			redisutils.SetKeyAndValueTime("AdminToken"+id, token, days*86400);
			redisutils.SetKeyAndValueTime(token, "AdminToken"+id, days*86400);
			
			data.setCode(200);
			data.setMessage("注册成功");
			Map map = new HashMap<String, String>();
			map.put("token", token);
			data.setData(map);
			
		}
		
		
		return data;
	}
	//通过token获取id
	@RequestMapping("GetIdByToken")
	public Integer GetIdByToken (String token) {
		
		System.out.println("GetIdByToken开始运行");
		System.out.println(token);
		//去redis查
		
		if(redisutils.GetValueTimeByKey(token)>0) {
			//代表存在 
			//查出来id
			String strid = redisutils.GetValueByKey(token);
			String id = strid.substring(5);
			System.out.println(strid);
			//需要判断时admintoken还是token
			if (!Character.isDigit(strid.charAt(5))){
				//为admintoken
				return 0;
			}else {
				System.out.println(strid.substring(5));
				return Integer.valueOf(strid.substring(5));
			}
			
		}
		return 0;
		
	}
	
	@RequestMapping("GetIdByAdminToken")
	public Integer GetIdByAdminToken (String token) {
		
		System.out.println("GetIdByAdminToken开始运行");
		System.out.println(token);
		//去redis查
		System.out.println(redisutils.GetValueTimeByKey(token));
		if(redisutils.GetValueTimeByKey(token)>0) {
			//代表存在 
			//查出来id
			String strid = redisutils.GetValueByKey(token);
			String id = strid.substring(10);
			System.out.println(strid);
			return Integer.valueOf(strid.substring(10));
		}
		return 0;
		
	}
	
	//登录时通过id生成token存到数据库中并返回
	@RequestMapping("GetTokenByid")
	public ResponseData GetTokenByid(Integer id,Integer days) {
		ResponseData data= new ResponseData();
		//获得token
		String token = String.valueOf(UUID.randomUUID())+ System.currentTimeMillis();
		//如果数据库有"Token"+id作为key的话 应该
		if(redisutils.IsExist("Token"+id)) {
			//查询原有token
			String dbtoken = redisutils.GetValueByKey("Token"+id);
			System.out.println(dbtoken);
			
			
			
			data.setCode(200);
			
			data.setMessage("登录成功");
			Map map = new HashMap<String, String>();
			map.put("token", dbtoken);
			data.setData(map);
			
		}
		else {
			//存储进redis 双向存储并设置过期时间
			
			redisutils.SetKeyAndValueTime("Token"+id, token, days*86400);
			redisutils.SetKeyAndValueTime(token, "Token"+id, days*86400);
			
			data.setCode(200);
			data.setMessage("注册成功");
			Map map = new HashMap<String, String>();
			map.put("token", token);
			data.setData(map);
			
		}
		
		
		return data;
	}
	//根据短信验证码 生成uuid返回给前端 前端带着uuid提交表单
	@RequestMapping("GetCodeUUidByCode")
	public String GetCodeUUidByCode(String code) {
		//先生成uuid 
		String uuid = System.currentTimeMillis()+Integer.valueOf(code)+"";
		
		redisutils.SetKeyAndValueTime(uuid, code, 60);
		redisutils.SetKeyAndValueTime(code, uuid, 60);
		return uuid;
		
	}
	//根据短信验证码和uuid查询是否符合规则
	@RequestMapping("CheckUUidAndCode")
	// 1为成功 0为失败 -1为超时
	public int CheckUUidAndCode(String UUid,String code) {
		if( redisutils.GetValueTimeByKey(code) <0) {
			return -1;
		}
		System.out.println(redisutils.GetValueByKey(code));
		System.out.println(UUid);
		if(redisutils.GetValueByKey(code).equals(UUid)) {
			return 1;
		}
		return 0;
	}
	
	

}
